JBoss提供的常用的对称加密算法

package com.test.resteasy;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Date;

import javax.ws.rs.core.MediaType;

import junit.framework.Assert;

import org.jboss.resteasy.jose.jwe.JWEBuilder;
import org.jboss.resteasy.jose.jwe.JWEInput;
import org.jboss.resteasy.jose.jws.JWSBuilder;
import org.jboss.resteasy.jose.jws.JWSInput;
import org.jboss.resteasy.jose.jws.crypto.RSAProvider;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.junit.Test;

public class User {
	private String name;
	private Integer age;
	private Date birth;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	public static void main(String[] args) throws Exception {
		File file = new File("");
		URI uri = file.toURI();
		try {
			URL url = uri.toURL();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

	}

	private static KeyPair keyPair = null;
	static {
		try {
			keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testRSAWithContentType() throws Exception {
		

		String encoded = new JWSBuilder()
				.contentType(MediaType.TEXT_PLAIN_TYPE)
				.content("Hello World", MediaType.TEXT_PLAIN_TYPE)
				.rsa256(keyPair.getPrivate());

		//System.out.println(encoded);

		JWSInput input = new JWSInput(encoded,
				ResteasyProviderFactory.getInstance());
		//System.out.println(input.getHeader());
		String msg = (String) input.readContent(String.class);
		Assert.assertEquals("Hello World", msg);
		Assert.assertTrue(RSAProvider.verify(input, keyPair.getPublic()));

	}

	@Test
	public void testRSA() throws Exception {
		//KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();

		String content = "Live long and prosper.";

		{
			PublicKey publicKey = keyPair.getPublic();
			RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
			String encoded = new JWEBuilder()
					.contentBytes(content.getBytes())
					.RSA1_5(rsaPublicKey);
			//System.out.println("encoded: " + encoded);
			byte[] raw = new JWEInput(encoded).decrypt(
					(RSAPrivateKey) keyPair.getPrivate()).getRawContent();
			String from = new String(raw);
			Assert.assertEquals(content, from);
		}
		{
			String encoded = new JWEBuilder().contentBytes(content.getBytes())
					.RSA_OAEP((RSAPublicKey) keyPair.getPublic());
			//System.out.println("encoded: " + encoded);
			byte[] raw = new JWEInput(encoded).decrypt(
					(RSAPrivateKey) keyPair.getPrivate()).getRawContent();
			String from = new String(raw);
			Assert.assertEquals(content, from);
		}
		{
			String encoded = new JWEBuilder().contentBytes(content.getBytes())
					.A128CBC_HS256().RSA1_5((RSAPublicKey) keyPair.getPublic());
			//System.out.println("encoded: " + encoded);
			byte[] raw = new JWEInput(encoded).decrypt(
					(RSAPrivateKey) keyPair.getPrivate()).getRawContent();
			String from = new String(raw);
			Assert.assertEquals(content, from);
		}
		{
			String encoded = new JWEBuilder().contentBytes(content.getBytes())
					.A128CBC_HS256()
					.RSA_OAEP((RSAPublicKey) keyPair.getPublic());
			//System.out.println("encoded: " + encoded);
			byte[] raw = new JWEInput(encoded).decrypt(
					(RSAPrivateKey) keyPair.getPrivate()).getRawContent();
			String from = new String(raw);
			Assert.assertEquals(content, from);
		}
	}

	@Test
	public void testDirect() throws Exception {
		String content = "Live long and prosper.";
		String encoded = new JWEBuilder().contentBytes(content.getBytes()).dir("geheim");
		//System.out.println("encoded: " + encoded);
		byte[] raw = new JWEInput(encoded).decrypt("geheim").getRawContent();
		String from = new String(raw);
		Assert.assertEquals(content, from);

	}
}


posted @ 2013-10-29 22:21  pangbangb  阅读(451)  评论(0编辑  收藏  举报